package pwd.allen.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import oracle.ucp.proxy.annotation.Post;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import pwd.allen.entity.User;
import pwd.allen.service.impl.UserService;
import pwd.allen.util.Pager;

import javax.websocket.server.PathParam;
import java.util.HashMap;

/**
 * @author 门那粒沙
 * @create 2021-06-19 15:27
 **/
@RequestMapping("user")
@RestController
@Api(tags = "我的控制器")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "保存", notes = "保存接口")
    @PostMapping("/save")
    public Object save(@RequestBody User user) {
        if (ObjectUtils.isEmpty(user.getName())) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        boolean rel = userService.save(user);
        HashMap<String, Object> map_rel = new HashMap<>();
        map_rel.put("status", rel);
        map_rel.put("user", user);
        return map_rel;
    }

    @PostMapping("/update")
    public Object update(@RequestBody User user) {
        if (ObjectUtils.isEmpty(user.getId())) {
            throw new IllegalArgumentException("ID不能为空");
        }
        boolean rel = userService.updateById(user);
        HashMap<String, Object> map_rel = new HashMap<>();
        map_rel.put("status", rel);
        map_rel.put("user", user);
        return map_rel;
    }

    @ApiOperation(value = "根据ID删除", notes = "根据ID删除")
    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        boolean rel = userService.removeById(id);
        HashMap<String, Object> map_rel = new HashMap<>();
        map_rel.put("status", rel);
        return map_rel;
    }

    @ApiOperation(value = "根据ID查询", notes = "根据ID查询")
    @GetMapping("/get/{id}")
    public Object get(@PathVariable("id") Integer id) {
        User user = userService.getById(id);
        HashMap<String, Object> map_rel = new HashMap<>();
        map_rel.put("status", 1);
        map_rel.put("user", user);
        return map_rel;
    }

    @ApiOperation(value = "分页", notes = "分页")
    @PostMapping("/pager")
    public Object pager(@RequestBody Pager<User> pager) {
        IPage<User> userIPage = userService.selectByPager(pager);
        return userIPage;
    }
}
